# import time
#
# from selenium import webdriver
#
# driver = webdriver.Chrome()# 指定浏览器
# time.sleep(2)
# #
# # driver.get('https://www.baidu.com/')   # get； 打开网页
# # time.sleep(4)
# path = r'https://www.baidu.com/'
#
# driver.get(path)
# time.sleep(2)
# # driver.set_window_size(1000,1000)   # set_window_size 设置网页的大小（宽，高）
# driver.maximize_window()   # 设置全屏
# time.sleep(3)
# # 打开B站
# driver.get('https://www.bilibili.com/')
# time.sleep(3)
# driver.back()   # 回退页面
# time.sleep(2)
#
# driver.forward()   # 前进页面
# time.sleep(2)
#
# driver.close()  # 关闭浏览器







# 获取 网页标题   网址    浏览器的名称     源码

#
# from selenium import  webdriver
#
#
# browser = webdriver.Chrome()
#
# browser.get('https://www.baidu.com/') # 打开百度

# 网页标题

# print(browser.title)

# 获取网址

# print(browser.current_url)

# 获取浏览器名称

# print(browser.name)

# 网页源码

# print(browser.page_source)



# import time
#
# from selenium import webdriver
# from selenium.webdriver.common.by import By
# from selenium.webdriver.common.keys import Keys
#
# driver = webdriver.Chrome()
#
# driver.get('https://www.baidu.com/')
#
# #  定位到搜索框并输入文本
#
# search_box = driver.find_element(by = By.ID,value='chat-textarea')     # 定位搜索框
#
# search_box.send_keys("selenium自动化测试")   #  模拟在元素中输入文本，输入 selenium自动化测试
#
# search_btn = driver.find_element(by = By.ID,value='chat-submit-button')  # 定位搜索按钮
#
# search_btn.click()  # click  点击
#
# time.sleep(5)







# 模拟鼠标的操作
# 导入actionchains类

# from selenium import webdriver
# from selenium.webdriver.common.by import By
# from selenium.webdriver.common.keys import Keys
# import time
# from selenium.webdriver.common.action_chains import ActionChains
#
# driver = webdriver.Chrome()
#
# # 设置全屏
# driver.maximize_window()
#
# # 创建ActionChains实例
# actions = ActionChains(driver)
# driver.get("https://www.baidu.com")
#
# # 用ID定位到设置链接
# settings = driver.find_element(By.ID, value="s-usersetting-top")
#
# # 鼠标悬停操作
# actions.move_to_element(settings).perform()   # 鼠标悬停到设置上
# time.sleep(10)
# print("鼠标已悬停在设置上")  # 设置会有弹窗下来




# 模拟键盘的操作



# from selenium import webdriver
# from selenium.webdriver.common.by import By
# from selenium.webdriver.common.keys import Keys
# import time
# from selenium.webdriver.common.action_chains import ActionChains
#
#
#
#
# driver = webdriver.Chrome()
#
# driver.get("https://www.baidu.com")
#
# driver.maximize_window()
# time.sleep(5)
#
# # 定位到百度的搜索框，输入内容
# driver.find_element('id','chat-textarea').send_keys("selenium自动化测试")
# # 定位到百度的搜索框并且按下回车键（代替点击搜索按钮）
# driver.find_element('id','chat-textarea').send_keys(Keys.ENTER)
# time.sleep(5)

# import time
# from selenium import webdriver
# from selenium.webdriver.common.keys import Keys
#
# driver = webdriver.Chrome()
# driver.get("https://www.baidu.com")
# driver.maximize_window()
# time.sleep(2)
#
# driver.find_element("id", "chat-textarea").send_keys("Selenium自动化测试")
# # 全选
# driver.find_element("id", "chat-textarea").send_keys(Keys.CONTROL, 'a')  # CONTROL 代表键盘上 ctrl 键
# time.sleep(3)
# # # 复制
# driver.find_element("id", "chat-textarea").send_keys(Keys.CONTROL, 'c')
# # # 粘贴
# driver.find_element("id", "chat-textarea").send_keys(Keys.CONTROL, 'v')
# time.sleep(5)
#



# 延迟等待

# 强制等待
# time.sleep(1)   # 效率低
# 隐式等待
# import time
# from selenium import webdriver
# from selenium.webdriver.common.keys import Keys
#
# driver = webdriver.Chrome()
# driver.get("https://www.baidu.com")
# driver.maximize_window()
# # time.sleep(2)
# driver.implicitly_wait(2)   # 隐式等待： 如果我们到了时间的话，还有元素的节点没有加载出来的话，会直接抛出异常
# # 显式等待；我们设置一个时间一个条件，在我们规定的时间里，每隔一段是按查看条件是否是成立的，成立，则继续运行，不成立 报错（超时）
#
#
# driver.find_element("id", "chat-textarea").send_keys("Selenium自动化测试")
# # 全选
# driver.find_element("id", "chat-textarea").send_keys(Keys.CONTROL, 'a')  # CONTROL 代表键盘上 ctrl 键
# time.sleep(3)
# # # 复制
# driver.find_element("id", "chat-textarea").send_keys(Keys.CONTROL, 'c')
# # # 粘贴
# driver.find_element("id", "chat-textarea").send_keys(Keys.CONTROL, 'v')
# time.sleep(5)


# from selenium.webdriver.support.ui import WebDriverWait
# from selenium.webdriver.support import expected_conditions as EC
# from selenium.webdriver.common.by import By
# from selenium import webdriver
# import time
# driver = webdriver.Chrome()
# driver.get("https://www.baidu.com")
#
# # 创建WebDriverWait实例，设置最大等待时间10秒，轮询间隔0.5秒
# wait = WebDriverWait(driver, timeout=10, poll_frequency=0.5)
#
# # 等待元素可见
# search_box = wait.until(EC.visibility_of_element_located((By.ID, "chat-textarea")))
# time.sleep(2)
# # 等待元素可点击
# search_btn = wait.until(EC.element_to_be_clickable((By.ID, "chat-submit-button")))
# time.sleep(2)
# # 输入搜索词
# search_box.send_keys("Selenium")
# time.sleep(2)
# search_btn.click()
#
# input('请按回车键关闭浏览器')
# driver.quit()
# import time
#
# from selenium import webdriver
# from selenium.webdriver.common.by import By
# from selenium.webdriver.common.keys import Keys
# from selenium.webdriver.common.action_chains import ActionChains
# #
# # # 搜索Selenium自动化测试得到网页1，再点击搜索出来的第一个网页得到网页2，在网页2再点击一个地方得到网页3，最后把网页2关掉。
#
# # 1、先进入到百度，并设置为全屏，设置全局等待缓冲6秒
# driver = webdriver.Chrome()
# driver.maximize_window()
# driver.get("https://www.baidu.com")
# driver.implicitly_wait(6)
#
# # 2、先定位到搜索框，输入Selenium自动化测试并按回车
# search_box = driver.find_element(by=By.ID, value="chat-textarea")
# search_box.send_keys("Selenium自动化测试" + Keys.ENTER)
# # time.sleep(3)
#
# # 3、先获取并保存当前（主窗口）的句柄
# main_window_handle = driver.current_window_handle
# print(main_window_handle)
#
# # 4、找到要点击的元素定位，并创建ActionChains实例，再点击一个网页
# search_box2 = driver.find_element(by=By.CLASS_NAME, value='tts-b-hl')
# actions = ActionChains(driver)
# actions.click(search_box2).perform()
#
# # 5、等待新窗口打开，并获取所有窗口句柄（有时候句柄可能不止两个）
# all_handles = driver.window_handles
# #
# # 6. 找到新窗口的句柄
# # 方法A： 循环遍历，找到不是主窗口句柄的那个（当有多个窗口时）
# for handle in all_handles:
#    if handle != main_window_handle:
#        new_window_handle = handle
#        break
# #
# # # # 方法B（更简单）： 直接假设新窗口是列表最后一个
# # # # new_window_handle = all_handles[-1]
# # #
# # 7. 切换到新窗口
# driver.switch_to.window(new_window_handle)
# print(f"已切换到新窗口，标题是: {driver.title}")
# #
# # 8. 切换之后现在可以在新窗口中进行操作了，找到元素并点击
# element_in_new_tab = driver.find_element(By.XPATH, '/html/body/div/div[1]/div/main/nav/ol/li[2]/a')
# element_in_new_tab.click()
# time.sleep(3)
# #
# # 8. 操作完成后，如果需要关闭新窗口并回到主窗口
# driver.close()  # 关闭当前（新）标签页
# driver.switch_to.window(main_window_handle)  # 将控制权交回主窗口
# print(f"已切换回主窗口，标题是: {driver.title}")
#
# input("按回车键关闭浏览器")  # 等待用户输入
# driver.quit()


# from selenium import webdriver
# from selenium.webdriver.common.by import By
# from selenium.webdriver.support.ui import WebDriverWait
# from selenium.webdriver.support import expected_conditions as EC
#
# driver = webdriver.Chrome()
# driver.get("your_page_url")  # 你的网站 URL
#
# # 通过id切换
# driver.switch_to.frame("loginFrame")  # 使用id
#
# # 或者通过name切换（如果iframe有name属性）
# driver.switch_to.frame("login")  # 使用name
#
# # 操作iframe内的元素
# username_field = WebDriverWait(driver, 10).until(
#     EC.presence_of_element_located((By.ID, "username"))
# )
# username_field.send_keys("testuser")
#
# # 切换回主文档
# driver.switch_to.default_content()


# from selenium import webdriver
# from selenium.webdriver.common.by import By
# from selenium.webdriver.support.ui import WebDriverWait
# from selenium.webdriver.support import expected_conditions as EC
# import time
#
# # 初始化浏览器驱动
# driver = webdriver.Chrome()
# driver.get("https://baike.baidu.com/item/Selenium/18266")  # 百度百科
#
# # 获取全页面高度（包括没有滑动到的部分），兼容不同浏览器
# total_height = driver.execute_script("return document.body.scrollHeight || document.documentElement.scrollHeight;")
# """
# document.body 返回 HTML的<body> 元素 .scrollHeight 返回元素的完整高度
# document.documentElement: 获取HTML文档的根元素<html> scrollHeight: 同样返回完整内容高度
# ||如果第一个值为0、null或undefined，则返回第二个值 ，为了浏览器兼容性，因为不同浏览器可能使用不同的属性
# """
# # 获取可视页面高度，并每次滚动一个视口高度的80%
# viewport_height = driver.execute_script("return window.innerHeight;")
# '''
# 获取视口的高度
#  window.innerHeight：返回浏览器窗口可视区域的高度
#
# '''
# scroll_step = viewport_height * 0.8
# # 0.8 (80%): 表示每次滚动视口高度的80%
#
# # 设置页面初始高度为0
# current_position = 0
#
# # 循环语句，当初始高度小于全页面高度时
# while current_position < total_height:
#     # scrollTo（x，y）滑动到指定坐标的位置
#     driver.execute_script(f"window.scrollTo(0, {current_position});")
#
#     # 没滑动一次current_position就加上页面的百分80%高度
#     current_position += scroll_step
#
#     # 暂停一下，模拟阅读速度
#     time.sleep(0.5)
# # driver.execute_script("window.scrollTo(0, 0);")  # 滚到页面顶部
#
# # 滚到页面特定位置（三分之一）
# # target_position = total_height // 3
# # driver.execute_script(f"window.scrollTo(0, {target_position});")
# # 最终确保滚动到底部
# driver.execute_script("window.scrollTo(0, document.body.scrollHeight || document.documentElement.scrollHeight);")
# print("已滚动到页面底部")
#
# # 等待2秒观察效果
# time.sleep(2)
import time

import driver as driver


#
#
#
# from selenium import webdriver
# from selenium.webdriver.common.by import By
# from selenium.webdriver.common.keys import Keys
# import time
# from selenium.webdriver.common.action_chains import ActionChains
#
# driver = webdriver.Chrome()
#
# # 设置全屏
# driver.maximize_window()
#
# # 创建ActionChains实例
# actions = ActionChains(driver)
# driver.get("https://www.baidu.com")
#
# # 用ID定位到设置链接
# settings = driver.find_element(By.ID, value="s-usersetting-top")
#
# # 鼠标悬停操作
# actions.move_to_element(settings).perform()   # 鼠标悬停到设置上
# time.sleep(3)
# driver.find_element(By.CLASS_NAME,'set').click()
# time.sleep(3)
# # driver.find_element_by_class_name('prefpanelgo setting-btn c-btn c-btn-primary').click()
# driver.find_element(By.CLASS_NAME,'prefpanelgo').click()
# time.sleep(3)
#
# alert = driver.switch_to.alert
# alert.accept()
#
#
# input('输入回车关闭浏览器')
#
# driver.quit()


#对弹窗进行取消操作
# alert = driver.switch_to.alert
# alert.dismiss()




# # 下拉框操作：
# from selenium.webdriver.support.select import Select
# from selenium import webdriver
# from selenium.webdriver.common.by import By
# from selenium.webdriver.common.keys import Keys
# import time
# from selenium.webdriver.common.action_chains import ActionChains
#
# # # 初始化浏览器驱动
# driver = webdriver.Chrome()
# driver.get("http://sahitest.com/demo/selectTest.htm")  # 百度百科
# x1 = driver.find_element(By.ID,'s2Id')   # 下拉框
# time.sleep(5)
# select1 = Select(x1)  # 进行实例化
# # select1.select_by_index(3)   # 根据下角标定位为
# select1.select_by_value("o1")   # 根据 value 进行定位
# select1.select_by_visible_text('o3')
#
# time.sleep(5)
#
#
# input('输入回车关闭浏览器')
#
# driver.quit()
#

# 在自动化的测试中，断言验证实际结果 是否符合预期结果



# 文本信息断言


# from selenium import webdriver
# from selenium.webdriver.common.by import By
# import time
# from selenium.webdriver.common.keys import Keys
#
# driver = webdriver.Chrome()
# driver.get('https://www.baidu.com')
#
# # 输入关键字 并搜索
#
# search_box = driver.find_element(by=By.ID, value="chat-textarea")
# search_box.send_keys("selenium_百度搜索" + Keys.ENTER)
# time.sleep(3)


# 断言文本
# page_text = driver.find_element(By.TAG_NAME,'body').text
# time.sleep(3)
# assert 'selenium'  in page_text   # assert(断言)

# 断言 title
# expected_title = 'selenium_百度搜索_百度搜索'
# actual_title = driver.title  # driver.title 返回的是标签中的内容
# assert expected_title == actual_title
# print('hhhh')
# input('按回车')
# driver.quit()
# time.sleep(2)
#
# current = driver.current_url  # 获取 URL
# print(current)
# time.sleep(2)
# assert "https://www.baidu.com" in current
# print('通了')
# driver.quit()
